package com.trt.eatmeat.socket;

import io.netty.channel.Channel;
import io.netty.channel.ChannelId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @Author yufeng_ma@trtjk.com
 * @Description //TODO
 * @Date 2022/1/20 19:28
 **/
public class UserChannelRel {
    public static Logger logger = LoggerFactory.getLogger(UserChannelRel.class);
    private static HashMap<ChannelId,String > manager = new HashMap<>();


    public static void put(String senderId, ChannelId channelId) {
        manager.put(channelId,senderId);
    }


    public static Channel get(String senderId) {
        ChannelId channelId =null;
        for (Map.Entry<ChannelId,String> entry:manager.entrySet()){
            if(senderId.equals(entry.getValue())){
                channelId = entry.getKey();
            }
        }
        if(channelId==null){
            return null;
        }
        return NettyServer.users.find(channelId);
    }

    public static Channel getOne() {
        if(manager.size()<1){
            return null;
        }
       List<ChannelId> list = new ArrayList<>(manager.keySet()) ;
        return NettyServer.users.find(list.get(0));
    }

    public static void output() {
        logger.info("用户个数"+manager.size());
        for (HashMap.Entry<ChannelId, String> entry : manager.entrySet()) {
            logger.info("UserId:" + entry.getValue() + ":,ChannelId:" + entry.getKey().asLongText());
        }
    }


    public static void remove(ChannelId channel) {
        if(manager.containsKey(channel)){
            manager.remove(channel);
        }
    }
}
